﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using SWCInvoice.Servicelayer;
using System.Globalization;
using Cyotek.GhostScript;
using SWCInvoice.Model;
using SWCInvoice.Utils;

namespace SWCInvoice.ViewModel {
    public class BackstageViewModel : ViewModelBase {

        #region Fields

        /// <summary>
        /// Helper to read/write secure password user stuff to the config
        /// </summary>
        WebConfigEncryption _webConfigEncryption;

        /// <summary>
        /// The application culture
        /// </summary>
        CultureInfo _culture;

        /// <summary>
        /// The PDF temporary view path
        /// </summary>
        string _temporaryViewPath;

        /// <summary>
        /// Gets or sets the basic result path name
        /// </summary>
        string _basicResultPathName;

        /// <summary>
        /// The PDF session path name 
        /// </summary>
        string _sessionPathName;

        /// <summary>
        /// The PDF merge path name
        /// </summary>
        string _mergePathName;

        /// <summary>
        /// The PDF documents path name
        /// </summary>
        string _documentsPathName;

        /// <summary>
        /// The preview image format
        /// </summary>
        ImageFormat _previewImageFormat;

        /// <summary>
        /// The preview image dpi
        /// </summary>
        int _previewImageDpi;

        /// <summary>
        /// The preview image antialias mode
        /// </summary>
        AntiAliasMode _previewImageAntiAliasMode;

        /// <summary>
        /// Use a proxy during output comunication
        /// </summary>
        bool _proxyUse;

        /// <summary>
        /// The proxy url
        /// </summary>
        string _proxy;

        /// <summary>
        /// The domain for the proxy
        /// </summary>
        string _domain;

        /// <summary>
        /// The user for the proxy
        /// </summary>
        string _user;

        /// <summary>
        /// The user password for the proxy
        /// </summary>
        string _password;

        #endregion

        #region Properties

        private WebConfigEncryption WebConfigEncryption
        {
            get {
                if (_webConfigEncryption != null)
                    return _webConfigEncryption;
                else {
                    _webConfigEncryption = new WebConfigEncryption();
                    return _webConfigEncryption;
                }
            }
            set {
                _webConfigEncryption = value;
            }
        }

        /// <summary>
        /// Gets or sets the application culture
        /// </summary>
        public CultureInfo Culture
        {
            get { return _culture; }
            set
            {
                if (_culture == value) return;
                _culture = value;
                RaisePropertyChanged("Culture");
                if (_culture != null)
                    SWCInvoiceSettings.SetUserSettingParameter("Culture", value.Name);
            }
        }

        /// <summary>
        /// Gets or sets the temporary view path
        /// </summary>
        public string TemporaryViewPath {
            get { return _temporaryViewPath; }
            set {
                if (_temporaryViewPath == value) return;
                _temporaryViewPath = value;
                RaisePropertyChanged("TemporaryViewPath");
                SWCInvoiceSettings.SetUserSettingParameter("TemporaryViewPath", value);
            }
        }


        /// <summary>
        /// Gets or sets the basic result path name
        /// </summary>
        public string BasicResultPathName
        {
            get { return _basicResultPathName; }
            set
            {
                if (_basicResultPathName == value) return;
                _basicResultPathName = value;
                RaisePropertyChanged("BasicResultPathName");
                SWCInvoiceSettings.SetUserSettingParameter("BasicResultPathName", value);
            }
        }

        /// <summary>
        /// Gets or sets the session path name
        /// </summary>
        public string SessionPathName
        {
            get { return _sessionPathName; }
            set
            {
                if (_sessionPathName == value) return;
                _sessionPathName = value;
                RaisePropertyChanged("SessionPathName");
                SWCInvoiceSettings.SetUserSettingParameter("SessionPathName", value);
            }
        }

        /// <summary>
        /// Gets or sets the merge path name
        /// </summary>
        public string MergePathName
        {
            get { return _mergePathName; }
            set
            {
                if (_mergePathName == value) return;
                _mergePathName = value;
                RaisePropertyChanged("MergePathName");
                SWCInvoiceSettings.SetUserSettingParameter("MergePathName", value);
            }
        }

        /// <summary>
        /// Gets or sets the merge path name
        /// </summary>
        public string DocumentsPathName
        {
            get { return _documentsPathName; }
            set
            {
                if (_documentsPathName == value) return;
                _documentsPathName = value;
                RaisePropertyChanged("DocumentsPathName");
                SWCInvoiceSettings.SetUserSettingParameter("DocumentsPathName", value);
            }
        }

        /// <summary>
        /// Gets or sets the preview image format
        /// </summary>
        public ImageFormat PreviewImageFormat {
            get { return _previewImageFormat; }
            set {
                if (_previewImageFormat == value) return;
                _previewImageFormat = value;
                RaisePropertyChanged("PreviewImageFormat");
                SWCInvoiceSettings.SetUserSettingParameter("PreviewImageFormat", value.ToString());
            }
        }

        /// <summary>
        /// Gets or sets the preview image dpi
        /// </summary>
        public int PreviewImageDpi {
            get { return _previewImageDpi; }
            set {
                if (_previewImageDpi == value) return;
                _previewImageDpi = value;
                RaisePropertyChanged("PreviewImageDpi");
                SWCInvoiceSettings.SetUserSettingParameter("PreviewImageDpi", value.ToString());
            }
        }

        /// <summary>
        /// Gets or sets the preview image antialias mode
        /// </summary>
        public AntiAliasMode PreviewImageAntiAliasMode {
            get { return _previewImageAntiAliasMode; }
            set {
                if (_previewImageAntiAliasMode == value) return;
                _previewImageAntiAliasMode = value;
                RaisePropertyChanged("PreviewImageAntiAliasMode");
                SWCInvoiceSettings.SetUserSettingParameter("PreviewImageAntiAliasMode", value.ToString());
            }
        }

        /// <summary>
        /// Use a proxy during output comunication
        /// </summary>
        public bool ProxyUse {
            get { return _proxyUse; }
            set {
                if (_proxyUse == value) return;
                _proxyUse = value;
                RaisePropertyChanged("ProxyUse");
                SWCInvoiceSettings.SetUserSettingParameter("ProxyUse", value.ToString());
            }
        }


        /// <summary>
        /// The proxy url
        /// </summary>
        /// <summary>
        /// Use a proxy during output comunication
        /// </summary>
        public string Proxy {
            get { return _proxy; }
            set {
                if (_proxy == value) return;
                _proxy = value;
                RaisePropertyChanged("Proxy");
                SWCInvoiceSettings.SetUserSettingParameter("Proxy", value.ToString());
            }
        }

        /// <summary>
        /// The domain for the proxy
        /// </summary>
        public string Domain {
            get {
                return WebConfigEncryption.Decrypt(_domain); }
            set {
                if (WebConfigEncryption.Decrypt(_domain) == value) return;
                _domain = WebConfigEncryption.Encrypt(value);
                RaisePropertyChanged("Domain");
                SWCInvoiceSettings.SetUserSettingParameter("Domain", WebConfigEncryption.Encrypt(value.ToString()));
            }
        }

        /// <summary>
        /// The user for the proxy
        /// </summary>
        public string User {
            get { return WebConfigEncryption.Decrypt(_user); }
            set {
                if (WebConfigEncryption.Decrypt(_user) == value) return;
                _user = WebConfigEncryption.Encrypt(value);
                RaisePropertyChanged("User");
                SWCInvoiceSettings.SetUserSettingParameter("User", WebConfigEncryption.Encrypt(value.ToString()));
            }
        }

        /// <summary>
        /// The user password for the proxy
        /// </summary>
        public string Password {
            get { return WebConfigEncryption.Decrypt(_password); }
            set {
                if (WebConfigEncryption.Decrypt(_password) == value) return;
                _password = WebConfigEncryption.Encrypt(value);
                RaisePropertyChanged("Password");
                SWCInvoiceSettings.SetUserSettingParameter("Password", WebConfigEncryption.Encrypt(value.ToString()));
            }
        }

        
        #endregion
    }
}
